package com.fc.tms.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.stereotype.Service;

import com.fc.framework.BusinessException;
import com.fc.system.service.impl.AbstractExcelReader;
import com.fc.tms.service.UserService;

@Service("dimissionReader")
public class DimissionReader extends AbstractExcelReader {

    @Resource
    UserService userService;

    @Override
    protected boolean readRow(Row row, int index) {
        if (index == 1)
            return false;

        Cell nameCell = row.getCell(0, Row.RETURN_BLANK_AS_NULL);
        if (nameCell != null) {
            String eName = nameCell.getStringCellValue().trim();

            if (StringUtils.isNoneEmpty(eName)) {
                Integer rowUpdatedNum = userService.dimission(eName);

                if (rowUpdatedNum != 1) {
                    throw new BusinessException("英文名错误！");
                }
                return false;
            }
        }

        return true;
    }

    @Override
    protected void validateExcel(Sheet sheet) {
        Cell firstCell = sheet.getRow(0).getCell(0, Row.RETURN_BLANK_AS_NULL);
        //首行
        if (firstCell == null) {
            throw new BusinessException("第一行数据为空!");
        }

        if (!"英文名".equals(firstCell.getStringCellValue().trim())) {
            throw new BusinessException("第一行第一个单元不是'英文名',请确认文档是否为离职人员");
        }
    }
}
